/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager.model;

import Golf_Manager.util.Util;
import Golf_Manager.vo.Member;
import Golf_Manager.vo.ValidVo;
import java.util.ArrayList;
import java.util.List; 
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Administrator
 */
public class MemberMngModel extends AbstractTableModel {

    private List<Golf_Manager.vo.Member> members = new ArrayList<>();
    private final String[] columns = {"이름", "생년월일", "나이",  "성별", "회사명", "핸디", "등급", "참여여부", "비고1", "비고2", "비고3"};
    private final boolean[] editables = {true, true, true, true, true, true, true, true, true, true, true};
    
    public MemberMngModel() {
    }

    @Override
    public int getRowCount() {
        return this.members.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }
    
    @Override
    public String getColumnName(int column) {
        return columns[column];
    }
    
    public Member getMember(int rowIndex){
        return this.members.get(rowIndex);
    }
    
    public List<Golf_Manager.vo.Member> getMembers(){
        return this.members;
    }

    public void setMembers(List<Member> members) {
        this.members = new ArrayList<>(members);
    }
    
    public void addMembers(List<Member> members) {
        this.members.addAll(members);
    }
    public void addMember(Member member){
        this.members.add(member);
        this.fireTableRowsInserted(this.members.size(), this.members.size());
    }
    
    public void delMember(int rowIndexm){
        this.members.remove(rowIndexm);
        this.fireTableRowsDeleted(rowIndexm, rowIndexm);
    }
    
    public void delMembers(){
        this.members = new ArrayList<>();
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        
        Object value = null;
        
        switch(columnIndex){
            case 0: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getName :" + this.members.get(rowIndex).getName()); 
                    value =  this.members.get(rowIndex).getName();
                    break;
            }
            case 1: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getBirth :" + this.members.get(rowIndex).getBirth());
                    value =  this.members.get(rowIndex).getBirth();
                    break;
            }         
            case 2: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getAge :" + this.members.get(rowIndex).getAge());
                    value = this.members.get(rowIndex).getAge();
                    break;
            }   
            case 3: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getGender :" + this.members.get(rowIndex).getGender());
                    value =  this.members.get(rowIndex).getGender();
                    break;
            } 
                    
            case 4: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getCompanyName :" + this.members.get(rowIndex).getCompanyName()); 
                    value =  this.members.get(rowIndex).getCompanyName();
                    break;
            }
            case 5: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getHand :" + this.members.get(rowIndex).getHand()); 
                    value =  this.members.get(rowIndex).getHand();
                    break;
            }
            case 6: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getGrade :" + this.members.get(rowIndex).getGrade()); 
                    value =  this.members.get(rowIndex).getGrade();
                    break;
            }
            case 7: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getJoinYn :" + this.members.get(rowIndex).getJoinYn()); 
                    value =  this.members.get(rowIndex).getJoinYn();
                    break;
            }
            
            case 8: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getJoinYn :" + this.members.get(rowIndex).getNote1()); 
                    value =  this.members.get(rowIndex).getNote1();
                    break;
            }
            case 9: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getJoinYn :" + this.members.get(rowIndex).getNote2()); 
                    value =  this.members.get(rowIndex).getNote2();
                    break;
            }
            case 10: {
                    //System.out.println("rowIndex : " + rowIndex + ", columnIndex : " + columnIndex + ": getJoinYn :" + this.members.get(rowIndex).getNote3()); 
                    value =  this.members.get(rowIndex).getNote3();
                    break;
            }
            
            default: value =  null; break;
        }
        return value;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //if(rowIndex >= this.members.size()) return;
        //System.out.println("setValueAt : " + rowIndex + ", " + columnIndex + " : " + "|" + aValue + "|");
        switch(columnIndex){
                case 0: {
                    this.members.get(rowIndex).setName(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;
                } 
                case 1: {
                    if(aValue != null && aValue.toString().trim().length() == 10){
                        this.members.get(rowIndex).setBirth(aValue.toString());
                        this.members.get(rowIndex).setAge(Util.getAgebyBirthDate(aValue.toString()));
                        this.members.get(rowIndex).setFlag("Y"); 
                    }
                    break;
                } 
                case 2: {
                    this.members.get(rowIndex).setAge(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;
                } 
                case 3: {
                    this.members.get(rowIndex).setGender(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break; 
                } 
                case 4: {
                    this.members.get(rowIndex).setCompanyName(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;    
                } 
                case 5: {
                    this.members.get(rowIndex).setHand(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;     
                } 
                case 6: {
                    this.members.get(rowIndex).setGrade(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;     
                } 
                case 7: {
                    this.members.get(rowIndex).setJoinYn(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;         
                } 
              
                case 8: {
                    this.members.get(rowIndex).setNote1(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;         
                } 
                case 9: {
                    this.members.get(rowIndex).setNote2(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;         
                } 
                case 10: {
                    this.members.get(rowIndex).setNote3(aValue == null ? null:  aValue.toString());
                    this.members.get(rowIndex).setFlag("Y"); 
                    break;         
                } 
                
                default: break;
            }
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return editables[columnIndex];
    }
    
    public ValidVo validCheckMember(){
        fireTableDataChanged();
        for(int row=0; row < this.members.size(); row++){
            Member member = this.getMember(row);
            ValidVo validVo = member.vlidCheck();
            
            if(validVo != null){
                validVo.setRowIndex(row);
                return validVo;
            } 
        }
        return null;
    }

}
